<template>
  <div class="user-wrapper">
    <div class="content-box">
      <a-dropdown>
        <span class="action ant-dropdown-link user-dropdown-menu">
          <a-avatar icon="user" style="background-color: #3B5998;margin-right:10px" />
          <span>{{ nickname }}</span>
        </span>
        <a-menu slot="overlay" class="user-dropdown-menu-wrapper">
          <a-menu-item key="1">
            <a @click="handleChangePass">
              <a-icon type="lock" />
              <span>修改密码</span>
            </a>
          </a-menu-item>
          <a-menu-item key="3">
            <a href="javascript:;" @click="handleLogout">
              <a-icon type="logout" />
              <span>退出登录</span>
            </a>
          </a-menu-item>
        </a-menu>
      </a-dropdown>
    </div>
    <a-modal :title="'修改密码'" :visible="visible" :centered="true" @cancel="handleCancel" :footer="null">
      <a-form :form="form" :loading="loading">
        <a-form-item v-bind="formItemLayout" label="旧密码" class="itemlayout">
          <a-input type="password" v-decorator="['OldPassword', { rules: [{ required: true, message: '请填写旧密码!' }] }]" />
        </a-form-item>
        <a-form-item v-bind="formItemLayout" label="新密码" class="itemlayout">
          <!-- <a-input type="password" v-decorator="['NewPassword', { rules: [{ required: true, message: '请填写新密码!' }] }]" /> -->
          <a-input type="password" v-decorator="['NewPassword', { rules: [{ required: true, message: '请填写密码!' },{pattern:regex,message: '密码需包含数字、大小写字母、特殊字符，长度8-20。'}] }]" />
              <span style="font-size: 12px;color: #666666">
                密码需包含数字、大小写字母、特殊字符，长度8-20。
              </span>
        </a-form-item>
        <a-form-item v-bind="formItemLayout" label="再次输入新密码" class="itemlayout">
          <a-input type="password"
            v-decorator="['NewAgainPassword', { rules: [{ required: true, message: '请填写新密码!' }] }]" />
        </a-form-item>
        <a-form-item v-bind="formItemLayout1" style="text-align:center;margin-bottom:0">
          <a-button type="primary" @click="ok">
            确定
          </a-button>
        </a-form-item>
      </a-form>
    </a-modal>
  </div>
</template>

<script>
import { mapActions, mapGetters } from 'vuex'
export default {
  name: 'UserMenu',
  components: {},
  data() {
    return {
      regex: /^(?=.*\d)(?!.*(\d)\1{2})(?!.*(012|123|234|345|456|567|678|789|987|876|765|654|543|432|321|210))(?=.*[a-z])(?=.*[A-Z])(?=.*[^\da-zA-Z\s]).{8,20}$/,
      visible: false,
      loading: false,
      form: this.$form.createForm(this),
      formItemLayout: {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 18 }
        }
      },
      formItemLayout1: {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 0 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 24 }
        }
      }
    }
  },
  computed: {
    ...mapGetters(['token', 'nickname'])
  },
  methods: {
    ...mapActions(['Logout', 'updatePassword']),
    handleLogout() {
      this.$confirm({
        title: '提示',
        content: '真的要注销登录吗 ?',
        centered: true,
        onOk: () => {
          return this.Logout({})
            .then(() => {
              setTimeout(() => {
                window.location="/"
              }, 16)
            })
            .catch(err => {
              this.$message.error({
                title: '错误',
                description: err.message
              })
            })
        },
        onCancel() { }
      })
    },
    handleChangePass() {
      this.visible = true
    },
    ok() {
      const {
        form: { validateFields }
      } = this
      validateFields((errors, values) => {
        if (!errors) {
          if (values['NewPassword'] != values['NewAgainPassword']) {
            this.$message.error('新密码不一致!')
            return
          }
          setTimeout(() => {
            this.loading = true
            const value = {
              OldPassword: values['OldPassword'],
              NewPassword: values['NewPassword'],
              // ID: this.currentId,
              // EmployeelID: this.empid,
              UserID: this.token.id
            }
            this.updatePassword(value)
            this.spinning = false
            this.visible = false
          }, 1000)
        } else {
        }
      })
    },
    handleCancel() {
      this.visible = false
    }
  }
}
</script>
